import sys
sys.path.append("../../")
from controller.MsgController import *

class ParseMiniteController:
    def __init__(self):
        self.msgcon = MsgController()
        self.zhangtingFlag = False
        self.dietingFlag = False

    def checkScore(self,code,codename,data,preClose,i,score):
        close = float(data["close"])
        zf = (close - preClose) / preClose
        print(score)
        return score + (zf * i)


    def checkRate(self,code,codename,data,preData,sendFlag):
        close = float(data["close"])
        preClose = float(preData["close"])
        zf = (close-preClose)*100/preClose
        if zf > 2:
            if sendFlag == True:
                self.sendMsg(data["curtime"], code, codename, "急速上涨" + str(round(zf,2)) + "%")
        elif zf < -2:
            if sendFlag == True:
                self.sendMsg(data["curtime"], code, codename, "急速下跌" + str(round(zf,2)) + "%")

    def checkMoney(self,code,codename,data,preData,flowMoney,sendFlag):
        curtime = data["curtime"][-5:]
        if curtime in ["09:31","11:30","13:01","15:00"]:
            return
        close = float(data["close"])
        preClose = float(preData["close"])
        money = float(data["money"])
        liangbi = round(money*100/flowMoney,2)
        if liangbi > 0.3:
            if close > preClose:
                if sendFlag == True:
                    self.sendMsg(data["curtime"], code, codename, "巨量拉升")
                #self.sendMsg(data["curtime"], code, codename, "巨量拉升" + str(liangbi) + "%")
            else:
                #self.sendMsg(data["curtime"], code, codename, "巨量杀跌" + str(liangbi) + "%")
                if sendFlag == True:
                    self.sendMsg(data["curtime"], code, codename, "巨量杀跌")


    def checkZhangting(self,code,codename,data,zhangtingPrice,sendFlag):
        if data["close"] == zhangtingPrice:
            if self.zhangtingFlag == False:
                self.zhangtingFlag = True
                if sendFlag == True:
                    self.sendMsg(data["curtime"],code,codename,"涨停封板")
        else:
            if self.zhangtingFlag == True:
                self.zhangtingFlag = False
                if sendFlag == True:
                    self.sendMsg(data["curtime"],code, codename, "打开涨停")

    def checkDieting(self,code,codename,data,dietingPrice,sendFlag):
        if data["close"] == dietingPrice:
            if self.dietingFlag == False:
                self.dietingFlag = True
                if sendFlag == True:
                    self.sendMsg(data["curtime"],code,codename,"跌停封板")
        else:
            if self.dietingFlag == True:
                self.dietingFlag = False
                if sendFlag == True:
                    self.sendMsg(data["curtime"],code,codename,"打开跌停")

    def run(self,priceinfo,miniteData):
        prePrice = float(priceinfo["f60"])
        zhangtingPrice = float(priceinfo["f51"])
        dietingPrice = float(priceinfo["f52"])
        code = priceinfo["f57"]
        codename = priceinfo["f58"]
        flowMoney = priceinfo["f117"]
        i = 0
        preRow = {}
        score = 0
        max = len(miniteData)-1
        for row in miniteData:
            if i == max:
                self.checkZhangting(code, codename, row, zhangtingPrice,True)
                self.checkDieting(code, codename, row, dietingPrice,True)
                #score = self.checkScore(code, codename, row, prePrice, i, score)
                self.checkMoney(code, codename, row, preRow, flowMoney,True)
                self.checkRate(code, codename, row, preRow,True)
            else:
                self.checkZhangting(code, codename, row, zhangtingPrice, False)
                self.checkDieting(code, codename, row, dietingPrice, False)
                # score = self.checkScore(code, codename, row, prePrice, i, score)
                if i > 0:
                    self.checkMoney(code, codename, row, preRow, flowMoney, False)
                    self.checkRate(code, codename, row, preRow, False)
            preRow = row
            i += 1

    def sendMsg(self,curtime,code,codename,content):
        msg = curtime +"，股票代码：" + code + "，股票名称：" + codename + "，"
        msg += content
        #self.msgcon.sendFeiShu(msg)
